<?php
/**
 * Created by PhpStorm.
 * User: liuchaozhong
 * Date: 2018/7/30
 * Time: 下午4:34
 * Op: 分类控制器
 */
namespace App\Admin\Controllers;

use App\Admin\Extensions\Category_isHide;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use App\Facades\AdminC;
use App\Http\Controllers\Controller;
use App\Models\Category;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;

class CategoryController extends Controller
{
    use ModelForm;

    /**
     * op: 列表页面
     */
    public function index()
    {
        return AdminC::content(function(Content $content){
            $content->header('分类');
            $content->description('列表');
            $content->body($this->grid());
        });
    }

    protected function grid()
    {
        return AdminC::grid(Category::class, function(Grid $grid) {
            $grid->filter(function($filter){
                $filter->like('name','分类名称');
                $model = new Category();
                $filter->equal('is_hide','显示/隐藏')->select($model->is_hide_arr);
            });
            if(!Input::get('_sort')){
                $grid->model()->orderBy('created_at','desc');
            }
            $grid->id('Id')->sortable()->style('width:10%;');
            $grid->name('分类')->style('width:20%;');
            $grid->pic('封面图')->display(function($pic){
                return '<img src="/uploads/'.$pic.'" width="120" height="80"/>';
            })->style('width:25%;');
            $grid->is_hide('状态')->display(function($is_hide){
                return $this->is_hide_arr[$is_hide];
            })->style('width:20%;');

            //操作
            $grid->actions(function($actions){
                $info =  $actions->row;
                if($info->is_hide){
                    $actions->prepend(new Category_isHide($actions->getKey(), 1));
                }else{
                    $actions->prepend(new Category_isHide($actions->getKey(), 0));
                }
            });

            //$grid->disableRowSelector();
            $grid->disableExport();
        });
    }

    protected function CategoryTree()
    {
        return Category::tree();
    }

    /**
     *  操作显示/隐藏
     */
    public function opreat()
    {
        $input = Input::all();
        $model = Category::find($input['id']);
        $model->is_hide = $input['type']==1 ? 0 : 1;
        $res = $model->save();
        if($res) {
            return response()->json([
                'code' => 1,
                'msg' => '成功'
            ]);
        }else{
            return response()->json([
                'code' => 0,
                'msg' => '失败'
            ]);
        }
    }

    /**
     * op: 创建
     */
    public function create()
    {
        return AdminC::content(function(Content $content){
            $content->header('分类');
            $content->description('添加');
            $content->body($this->form());
        });
    }

    /**
     * op: 编辑
     * @param $id 主键id
     */
    public function edit($id)
    {
        return AdminC::content(function(Content $content) use ($id){
            $content->header('分类');
            $content->description('编辑');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * op: 表单
     * @return mixed
     */
    protected function form()
    {
        return AdminC::form(Category::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->text('name','分类名称');
            $form->image('pic','封面图');
            $form->display('created_at', '创建时间');
            $form->display('updated_at', '更新时间');
        });
    }

}
